package com.irdstudio.efp.esb.api.service.yed.loanManage;

import com.alibaba.fastjson.JSON;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.EsbRetCodeStandard;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.TraceUtil;
import com.irdstudio.basic.framework.redis.redisutil.RedisUtil;
import com.irdstudio.efp.console.common.YedEnums;
import com.irdstudio.efp.esb.api.bean.yed.req.NlsCreditOrApplOnWayReqBean;
import com.irdstudio.efp.esb.api.bean.yed.resp.NlsCreditOrApplOnWayRespBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.rest.yed.req.ReqLoanPerformanceBean;
import com.irdstudio.efp.esb.api.rest.yed.resp.RespLoanPerformanceBean;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.irdstudio.basic.framework.redis.redisutil.keys.Ocm_Nls_Key.ESBRESPSERVICEBEAN_KEY;

@Service
public class YedLoanPerformanceService implements FrameworkService {

    // 日志信息
    private static final Logger logger = LoggerFactory.getLogger(YedLoanPerformanceService.class);

    // redis服务
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 需求编号:
     * 开发人员: zhangxc
     * 创建日期:2020年8月7日17:37:30
     * 功能描述:
     * 修改描述:
     */
    public EsbRespServiceBean processYedLoanPerformance(ReqLoanPerformanceBean reqWsdCreditBean, EsbReqServiceBean esbReqServiceBean, String perfcAtchBrch,String perfcAtchPrsn,String outAcctBrch,String orgName) throws ESBException {
        try {
            // 校验必要的信息
            validateYedReqInfo(reqWsdCreditBean);
            // 插入返回信息
            RespLoanPerformanceBean successRespBean = getSuccessRespBean(perfcAtchBrch,perfcAtchPrsn,outAcctBrch,orgName);
            // 清除否则影响后续流程
            TraceUtil.clear();
            EsbRespServiceBean esbRespServiceBean = EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean, successRespBean);
            setEsbRespServiceBean(reqWsdCreditBean.getPdCd() + "_" + reqWsdCreditBean.getIdentNo(), esbRespServiceBean);
            return esbRespServiceBean;
        } catch (ESBException e) {
            throw new ESBException(e.getErrorCode(), e.getMessage(), e);
        }
    }

    public void setEsbRespServiceBean(String key, EsbRespServiceBean esbRespServiceBean) {
        redisUtil.set(ESBRESPSERVICEBEAN_KEY.getPrefix() + key, JSON.toJSONString(esbRespServiceBean));
    }


    /**
     * 开发人员: zhangxc
     * 创建日期:2020年8月7日17:37:30
     * 功能描述:获取失败交易返回Bean
     * 修改描述:
     */
    public RespLoanPerformanceBean getFailureRespBean(String code, String msg) {
        RespLoanPerformanceBean nlsWsdFirstRespBean = new RespLoanPerformanceBean();
        nlsWsdFirstRespBean.setRetCd(code);
        nlsWsdFirstRespBean.setRetMsg(msg);
        nlsWsdFirstRespBean.setPerfcAtchPrsn("");
        nlsWsdFirstRespBean.setPerfcAtchBrch("");
        return nlsWsdFirstRespBean;
    }

    /**
     * 开发人员: zhangxc
     * 创建日期:2020年8月7日17:37:30
     * 功能描述:获取成功交易返回Bean
     * 修改描述:
     */
    public RespLoanPerformanceBean getSuccessRespBean(String perfcAtchBrch,String perfcAtchPrsn,String outAcctBrch,String orgName) {
        RespLoanPerformanceBean respLoanPerformanceBean = new RespLoanPerformanceBean();
        respLoanPerformanceBean.setRetCd(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE());
        respLoanPerformanceBean.setRetMsg("交易成功");
        respLoanPerformanceBean.setPerfcAtchBrch(perfcAtchBrch);
        respLoanPerformanceBean.setPerfcAtchPrsn(perfcAtchPrsn);
        respLoanPerformanceBean.setOutAcctBrch(outAcctBrch);
        respLoanPerformanceBean.setOutAcctBrchNm(orgName);
        return respLoanPerformanceBean;
    }

    /**
     * 开发人员: zhangxc
     * 创建日期:2020年8月7日17:37:30
     * 功能描述:验证请求参数
     * 修改描述:
     */
    private void validateYedReqInfo(ReqLoanPerformanceBean reqWsdCreditBean) throws ESBException {
        String pdNm = reqWsdCreditBean.getPdCd();
        if(StringUtils.isBlank(pdNm))
        {
            throw new ESBException(EsbRetCodeStandard.KHXXJYBTG.getValue(), "产品编号不能为空!!!");
        }
        if (!YedEnums.PrdIdEnum.YED.getValue().equals(pdNm)) {
            logger.error("查询产品编号【REQUESTID】:+"+pdNm+" 不是优e贷产品!!!");
            throw new ESBException(EsbRetCodeStandard.CPXXJYBTG.getValue(), "产品编号不正确!!!");
        }
        if(StringUtils.isBlank(reqWsdCreditBean.getIdentTp()))
        {
            throw new ESBException(EsbRetCodeStandard.KHXXJYBTG.getValue(), "证件类型不能为空!!!");
        }
        if(StringUtils.isBlank(reqWsdCreditBean.getIdentNo()))
        {
            throw new ESBException(EsbRetCodeStandard.KHXXJYBTG.getValue(), "证件号码不能为空!!!");
        }

        try {
            // 身份证校验
            MessageRuleCheckUtil.validateIdNo18(reqWsdCreditBean.getIdentNo(), EsbRetCodeStandard.KHXXJYBTG.getValue());
        } catch (BizException e) {
            logger.error("查询在途授信【REQUESTID】:+"+reqWsdCreditBean.getIdentNo()+" 证件号码格式错误!!!");
            throw new ESBException(EsbRetCodeStandard.JKYDCSJYBTG.getValue(), "证件号码格式错误!!!");
        }


    }
}
